package com.kzd.btgame.main.access

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.kzd.btgame.app.SPSConstance
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.databinding.FragmentAccessPhoneVerifyBinding
import com.kzd.btgame.jetpack.vm.AccessViewModel
import com.kzd.btgame.utils.countdown
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel
import kotlinx.coroutines.Job

class AccessPhoneVerifyFragment : OwnerViewBindingFragment<FragmentAccessPhoneVerifyBinding>() {

    companion object {
        fun newInstance(): AccessPhoneVerifyFragment {
            return AccessPhoneVerifyFragment()
        }
    }

    private var mCountdownJob: Job? = null

    private val mViewModel: AccessViewModel by lazy {
        ownerViewModel(requireActivity())
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> FragmentAccessPhoneVerifyBinding
        get() = FragmentAccessPhoneVerifyBinding::inflate

    override fun initView() {

        mViewBinding.etAccessPhoneVerifyPhone.hint = SPSConstance.userInfo?.mobile

        mViewBinding.etAccessPhoneVerifyPhone.addTextChangedListener {
            mViewBinding.tvAccessPhoneVerifySubmit.isEnabled = !it.isNullOrEmpty() &&
                    !mViewBinding.etAccessPhoneVerifyCode.text.isNullOrEmpty()
            mViewBinding.tvAccessPhoneVerifyGetCode.isEnabled =
                !it.isNullOrEmpty() && !(mCountdownJob?.isActive ?: false)
        }
        mViewBinding.etAccessPhoneVerifyCode.addTextChangedListener {
            mViewBinding.tvAccessPhoneVerifySubmit.isEnabled = !it.isNullOrEmpty() &&
                    !mViewBinding.etAccessPhoneVerifyPhone.text.isNullOrEmpty()
        }

        mViewBinding.tvAccessPhoneVerifyGetCode.setOnClickAntiShakeListener {
            mViewModel.accessGetCode(mViewBinding.etAccessPhoneVerifyPhone.text.toString(),
                type = 2)
        }

        mViewBinding.tvAccessPhoneVerifySubmit.setOnClickAntiShakeListener {
            mViewModel.accessVerifyPhone(
                mViewBinding.etAccessPhoneVerifyPhone.text.toString(),
                mViewBinding.etAccessPhoneVerifyCode.text.toString()
            )
        }

    }

    override fun initViewMode() {
        mViewModel.accessGetCodeLiveData.observe(this as LifecycleOwner, { response ->
            if (response == null) {
                return@observe
            }
            if (response.status != Status.SUCCESS)
                return@observe
            startCodeDownTime()
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        mCountdownJob?.cancel()
        mViewModel.accessGetCodeLiveData.postValue(null)
    }

    /**
     * 获取验证码倒计时
     * */
    private fun startCodeDownTime() {
        mCountdownJob = countdown(60, lifecycleScope,
            onTick = { second ->
                mViewBinding.tvAccessPhoneVerifyGetCode.text = "剩${second}秒"
            }, onStart = {
                mViewBinding.tvAccessPhoneVerifyGetCode.isEnabled = false
            }, onFinish = {
                mViewBinding.tvAccessPhoneVerifyGetCode.text = "发送验证码"
                mViewBinding.tvAccessPhoneVerifyGetCode.isEnabled =
                    !mViewBinding.etAccessPhoneVerifyPhone.text.isNullOrEmpty()
                mCountdownJob?.cancel()
            })
    }

}